package com.house.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.house.common.pojo.EUDataGridResult;
import com.house.mapper.HouseuserMapper;
import com.house.pojo.Houseuser;
import com.house.pojo.HouseuserExample;
import com.house.pojo.HouseuserExample.Criteria;
import com.house.service.HouseuserService;

@Service
public class HouseuserServiceImpl implements HouseuserService {
	@Autowired
	private HouseuserMapper houseuserMapper;
	@Override
	public Houseuser getUserById(int id) {
		// TODO Auto-generated method stub
		return houseuserMapper.selectByPrimaryKey(id);
	}

	@Override
	public EUDataGridResult getUserList(int page, int rows) {
		
		HouseuserExample example = new HouseuserExample();
		//设置分页
		PageHelper.startPage(page, rows);
		
		List<Houseuser> list = houseuserMapper.selectByExample(example);
		//取分页信息
		PageInfo<Houseuser> pageInfo = new PageInfo<>(list);

		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public int add(Houseuser user) {
		// TODO Auto-generated method stub
		return houseuserMapper.insert(user);
	}

	@Override
	public int delete(List<Integer> id) {
		// TODO Auto-generated method stub
		HouseuserExample example = new HouseuserExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(id);		
		return houseuserMapper.deleteByExample(example);
	}

	@Override
	public Houseuser findUserByuserIdanduserHousenum(String userId, Integer userHousenum) throws Exception {
		HouseuserExample example = new HouseuserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserHousenumEqualTo(userHousenum);
		criteria.andUserIdEqualTo(userId);
		List<Houseuser> houseuserslist = houseuserMapper.selectByExample(example);
		if(houseuserslist !=null & houseuserslist.size() > 0){
			Houseuser houseuser = houseuserslist.get(0);
			return houseuser;
		}
		return null;
	}


	@Override
	public void addExcle(List<Houseuser> list) {
		// TODO Auto-generated method stub
		for(int i=0;i<list.size();i++){
			houseuserMapper.insert(list.get(i));
		}
		 
	}}
